package com.ckzp.jfinal.system.controller;

import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.system.model.SysCode;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.system.cache.SysConfigCache;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.system.model.SysConfig;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2020/6/1.
 */
public class SysConfigController extends BaseController {

    @Admins(roles = "system", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    @Admins(roles = "system", name = "默认", log = false)
    public void SysConfigList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysConfigList.html");
    }

    /**
     * 列表数据提供API
     */
    @Admins(roles = "system", name = "默认", log = false)
    public void SysConfigListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_config").orderby(getRequest(), "sindex");
        Page<SysConfig> pages = ps.paginate(SysConfig.dao);
        List<SysConfig> datalist = pages.getList();
        for (SysConfig st : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加
     */
    @Admins(roles = "system", name = "添加系统配置页面", log = false)
    public void AddSysConfig() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("cf", new SysConfig());
        setAttr("form", fd);
        render("ModifySysConfig.html");
    }

    /**
     * 修改页面
     */
    @Admins(roles = "system", name = "修改系统配置页面", log = false)
    public void EditSysConfig() {
        String cf_key = getPara("cf_key", "");
        SysConfig o = SysConfig.dao.findById(cf_key);
        FormData fd = FormData.dao().setActions("EDIT");
        if (o != null) {
            int cf_type = o.getInt("cf_type");
            String cf_value = o.getStr("cf_value");
            String cf_source = o.getStr("cf_source");
            if (cf_type == 9) {//选择框
                fd.addData("cfvalueHtml", SysConfigCache.getSelectHtml(cf_source, cf_value));
            } else {
                fd.addData("cfvalueHtml", SysConfigCache.getInputHtml(cf_value));
            }
        }
        setAttr("form", fd);
        setAttr("cf", o);
        render("ModifySysConfig.html");
    }

    /**
     * 修改API
     */
    @Admins(roles = "system", name = "修改系统配置API", log = false)
    public void ModifySysConfigApi() {
        //取得数据
        Datas data = getDatas(getPara("postdata"));
        String cf_key = data.getStr("cf_key");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysConfig.dao.findById(cf_key)
                    .set("cf_value", data.getStr("cf_value"))
                    .set("cf_desc", data.getStr("cf_desc"))
                    .update();
            SysConfigCache.reload();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysConfig()
                    .set("cf_key", data.getStr("cf_key"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改field API
     */
    @Admins(roles = "system", name = "修改系统配置field API", log = false)
    public void ModifySysConfigFieldApi() {
        int cf_key = getParaToInt("cf_key", 0);
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysConfig.dao.findById(cf_key).set(field, value).update();
        renderJson(JsonResult.n(0, "修改成功"));
    }

    /**
     * 清除cache
     */
    @Admins(roles = "system", name = "清除系统配置cacheAPI", log = false)
    public void ReloadSysConfigApi() {
        SysConfigCache.reload();
        renderJson(JsonResult.n(0, "清除成功"));
    }


    /**
     * 删除系统配置接口API
     */
    @Admins(roles = "system", name = "删除系统配置接口API", log = false)
    public void DelSysConfigApi() {
        int cf_key = getInt("cf_key", 0);
        SysConfig demo = SysConfig.dao.findById(cf_key);
        if (demo == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (demo.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
        ///////////////////////////////////
//
//        int cf_key = getParaToInt("cf_key", 0);
//        int counts = Db.queryInt("select count(*) from TABLENAME where cf_key=?", cf_key);
//        if (counts == 0) {
//            if (SysConfig.dao.findById(cf_key).delete()) {
//                renderJson(JsonResult.getResult(0, "删除成功"));
//            } else {
//                renderJson(JsonResult.getResult(9, "删除失败"));
//            }
//        } else {
//            renderJson(JsonResult.getResult(9, "该记录下有" + counts + "子记录,不能删除"));
//        }
    }

}

